package com.kgc.smbms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.smbms.mapper.SmbmsBillMapper;
import com.kgc.smbms.mapper.SmbmsProviderMapper;
import com.kgc.smbms.pojo.SmbmsBill;
import com.kgc.smbms.pojo.SmbmsBillExample;
import com.kgc.smbms.pojo.SmbmsProvider;
import com.kgc.smbms.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmbmsBillServiceImpl implements SmbmsBillService {
    //引入对应的mapper对象
    @Autowired
    private SmbmsBillMapper smbmsBillMapper;

    @Autowired
    private SmbmsProviderMapper smbmsProviderMapper;


    @Override
    public PageInfo<SmbmsBill> selectBillByDondetion(String productname, Long providerid, Integer ispayment, Integer pageNo, Integer pageSize) {

        //实现步骤：
        //1.查询订单信息
        SmbmsBillExample smbmsBillExample = new SmbmsBillExample();
        SmbmsBillExample.Criteria criteria = smbmsBillExample.createCriteria();
        //做条件拼接，需要进行判断，如果传递的条件的值是默认值，那么就不拼接条件
        if(null != productname && !productname.equals("")){
            criteria.andProductnameLike("%"+productname+"%");
        }
        if(null != providerid && providerid != 0){
            criteria.andProvideridEqualTo(providerid);
        }

        if(null != ispayment && ispayment != 0){
            criteria.andIspaymentEqualTo(ispayment);
        }

        //在查询之前要开启mybatis的分页
        PageHelper.startPage(pageNo,pageSize);
        //开始查询
        List<SmbmsBill> smbmsBills = smbmsBillMapper.selectByExample(smbmsBillExample);

        //遍历所有的订单信息
        for (SmbmsBill smbmsBill : smbmsBills) {
            //2.根据查询的订单信息中的供应商编号，去查询对应的供应商信息
            SmbmsProvider smbmsProvider = smbmsProviderMapper.selectByPrimaryKey(smbmsBill.getProviderid());

            //3.把查询到的供应商信息的名称存储到SmbmsBill对象中
            smbmsBill.setProname(smbmsProvider.getProname());
        }

        //把查询结果放入到分布工具中
        PageInfo<SmbmsBill> pageInfo = new PageInfo<SmbmsBill>(smbmsBills);


        return pageInfo;
    }

    @Override
    public int addBill(SmbmsBill bill) {
        return smbmsBillMapper.insertSelective(bill);
    }

    @Override
    public int deleteBill(Long id) {
        return smbmsBillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmbmsBill selectBillById(Long id) {
        //1.根据id查询订单信息
        SmbmsBill smbmsBill = smbmsBillMapper.selectByPrimaryKey(id);
        //2.根据查询的订单对象中的供应商id查询出对应的供应商信息
        SmbmsProvider smbmsProvider = smbmsProviderMapper.selectByPrimaryKey(smbmsBill.getProviderid());
        //把供应商的名称存储到bill对象中
        smbmsBill.setProname(smbmsProvider.getProname());

        return smbmsBill;
    }

    @Override
    public int updateBill(SmbmsBill bill) {
        //调用这个方法表示以主键做为修改条件
        //当其他的属性的值为null的时候，这个值不休息
        return smbmsBillMapper.updateByPrimaryKeySelective(bill);
    }
}
